package com.ternnetwork.commons.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64Utils {

	
	   public static void main(String[] args) {  
	  
	    
	      
	    }  
	    //图片转化成base64字符串  
	    @SuppressWarnings("restriction")
		public static String imageTobase64(String imgFile){//将图片文件转化为字节数组字符串，并对其进行Base64编码处理  
	      
	        InputStream in = null;  
	        byte[] data = null;  
	        //读取图片字节数组  
	        try{  
	            in = new FileInputStream(imgFile);          
	            data = new byte[in.available()];  
	            in.read(data);  
	            in.close();  
	        }catch (IOException e){  
	            e.printStackTrace();  
	        }  
	        //对字节数组Base64编码  
	        BASE64Encoder encoder = new BASE64Encoder();  
	        return encoder.encode(data);
	    }  
	      
	    //base64字符串转化成图片  
	    @SuppressWarnings("restriction")
		public static boolean base64toImage(String imgStr,String imgFilePath){  
	        if (imgStr == null){ 
	            return false;  
	        }
	        BASE64Decoder decoder = new BASE64Decoder();  
	        try{  
	            //Base64解码  
	            byte[] b = decoder.decodeBuffer(imgStr);  
	            for(int i=0;i<b.length;++i){  
	                if(b[i]<0){//调整异常数据  
	                    b[i]+=256;  
	                }  
	            }  
	            OutputStream out = new FileOutputStream(imgFilePath);      
	            out.write(b);  
	            out.flush();  
	            out.close();  
	            return true;  
	        }catch (Exception e){  
	        	e.printStackTrace();
	            return false;  
	        }  
	    }  
	    
	public static InputStream base64ToInputStream(String base64String) {

		ByteArrayInputStream stream = null;

		try {

			@SuppressWarnings("restriction")
			BASE64Decoder decoder = new BASE64Decoder();

			@SuppressWarnings("restriction")
			byte[] bytes1 = decoder.decodeBuffer(base64String);

			stream = new ByteArrayInputStream(bytes1);

		} catch (Exception e) {

			// TODO: handle exception
		}
		return stream;
	}

	 
	
    /**
     * 本地图片转成BufferedImage流对象。需要一个参数，base64编码的字符串，可由步骤一得到。
     * @param base64string
     * @return
     */
	public static BufferedImage base64ToBufferedImage(String base64String) {

		BufferedImage image = null;

		try {

			InputStream stream = base64ToInputStream(base64String);

			image = ImageIO.read(stream);

		} catch (IOException e) {
			e.printStackTrace();
		}
		return image;
	}
	/**
	 * 将BufferedImage转为Base64
	 * @param bufferedImage
	 * @return
	 * @throws Exception
	 */
	public static String bufferedImageToBase64(BufferedImage bufferedImage) throws Exception {
		
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

		ImageIO.write(bufferedImage, "png", byteArrayOutputStream);

		byte[] bytes = byteArrayOutputStream.toByteArray();

		@SuppressWarnings("restriction")
		BASE64Encoder encoder = new BASE64Encoder();
		
		@SuppressWarnings("restriction")
		String imageBase64 = encoder.encodeBuffer(bytes).trim();
		
		// 删除 \r\n
		imageBase64 = imageBase64.replaceAll("\n", "").replaceAll("\r", "");
		
		return imageBase64;
	}
}
